package com.lzw.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzw.blog.admin.mapper.MessageBoardMapper;
import com.lzw.blog.admin.model.params.PageParam;
import com.lzw.blog.admin.pojo.MessageBoard;
import com.lzw.blog.admin.pojo.Tag;
import com.lzw.blog.admin.vo.PageResult;
import com.lzw.blog.admin.vo.Result;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageBoardService {

    @Autowired
    private MessageBoardMapper messageBoardMapper;

    public Result listMessageBoard(PageParam pageParam) {
        Page<MessageBoard> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        LambdaQueryWrapper<MessageBoard> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {
            queryWrapper.like(MessageBoard::getNickname, pageParam.getQueryString());
        }
        Page<MessageBoard> messageBoardPage = messageBoardMapper.selectPage(page, queryWrapper);

        PageResult<MessageBoard> pageResult = new PageResult<>();
        pageResult.setList(messageBoardPage.getRecords());
        pageResult.setTotal(messageBoardPage.getTotal());
        return Result.success(pageResult);
    }

    public Result add(MessageBoard messageBoard) {
        this.messageBoardMapper.insert(messageBoard);
        return Result.success(null);
    }

    public Result update(MessageBoard messageBoard) {
        this.messageBoardMapper.updateById(messageBoard);
        return Result.success(null);
    }

    public Result delete(Long id) {
        this.messageBoardMapper.deleteById(id);
        return Result.success(null);
    }

}
